package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.AliyunVodSDKUtils;
import com.atguigu.vod.utils.ConstantVideoObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Classname VodController
 * @Description TODO
 * @Date 2022/9/3 14:14
 * @Created by ZHL17
 */
@RestController
@RequestMapping("/eduvod/video")
public class VodController {
    @Autowired
    private VodService vodService;

    @PostMapping("uploadaliyunVideo")
    public R uploadVideo(@RequestParam("file")MultipartFile file){
        String videoId=vodService.uploadVideo(file);
        return R.ok().data("videoId",videoId);
    }
    @DeleteMapping("deleteVideoById/{id}")
    public R deleteVideoById(@PathVariable String id){
        vodService.deleteVideoById(id);
        return R.ok();
    }
    @DeleteMapping("deleteVideoList")
    public R deleteVideoList(@RequestParam List<String> videoIdList){
        vodService.removeVideoList(videoIdList);
        return R.ok();
    }
    @GetMapping("getPlayAuth/{videoId}")
    public R getPlayAuth(@PathVariable String videoId){
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantVideoObject.KEY_ID, ConstantVideoObject.KEY_SECRET);
        //建立请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        //响应
        GetVideoPlayAuthResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        //得到播放凭证
        String playAuth = response.getPlayAuth();
        return R.ok().data("playAuth",playAuth);

    }
}
